package org.example.cloudphoto.manager.websocket.picture.disruptor;

import com.lmax.disruptor.WorkHandler;
import lombok.extern.slf4j.Slf4j;
import org.example.cloudphoto.manager.websocket.picture.model.PictureEditRequestMessage;
import org.example.cloudphoto.manager.websocket.picture.strategy.PictureEditMessageStrategy;
import org.example.cloudphoto.manager.websocket.picture.strategy.PictureEditMessageStrategyFactory;
import org.example.cloudphoto.model.User;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;

@Slf4j
@Component
public class PictureEditEventWorkHandler implements WorkHandler<PictureEditEvent> {

    @Resource
    private PictureEditMessageStrategyFactory pictureEditMessageStrategyFactory;

    @Override
    public void onEvent(PictureEditEvent event) throws Exception {
        PictureEditRequestMessage pictureEditRequestMessage = event.getPictureEditRequestMessage();
        WebSocketSession session = event.getSession();
        User user = event.getUser();
        Long pictureId = event.getPictureId();
        // 获取到消息类别
        String type = pictureEditRequestMessage.getType();
        // 获取并执行对应的策略
        PictureEditMessageStrategy strategy = pictureEditMessageStrategyFactory.getStrategy(type);
        strategy.handle(pictureEditRequestMessage, session, user, pictureId);
    }
}
